function* test(num) {
    let x = yield num * 2;
    let y = yield x * 2;
    return y;
}

const gen = test(2);
const v1 = gen.next();
const v2 = gen.next(v1.value);
const v3 = gen.next(v2.value);
console.log(v3);

function spawn(genFunc, ...args) {
    return new Promise((resolve, reject) => {
        var gen = genFunc(...args);

        function step(nextFunc) {
            try {
                var next = nextFunc();
                console.log
            }
            catch (e) {
                return reject(e)
            }

            if (next.done) {
                return resolve(next.value);
            }

            Promise.resolve(next.value).then(function (v) {
                step(function() { return gen.next(v) });
            }, function(e) {
                step(function() { return gen.throw(e) })
            })
        }

        step(function() { return gen.next(undefined) })
    })
}

spawn(test, 2).then(res => {
    console.log(res)
})